﻿@using umbraco.BusinessLogic;
@using umbraco.cms.businesslogic.web;
@using umbraco;
@using System.Xml.XPath;
@using System.Net.Mail;
@{
    string name = "Full Name*";
    string email = "E-mail*";
    string telephone = "Telephone";
    string message = "Requirment/Comment*";
    
    string tip = "";
    if(IsPost)
    {
        // Retrieve the messages that the user entered.
        name = Request["text_name"];
        email = Request["text_email"];
        message = Request["text_message"];
        telephone = Request["text_telephone"];

        XPathNodeIterator currentNode=library.GetXmlNodeCurrent();
        string xmlString = currentNode.Current.InnerXml.ToString();
        if(xmlString == null || xmlString == "")
        {
            try
            {
                //create the mail message
                MailMessage mail = new MailMessage();
                //set the addresses
                mail.To.Add(System.Configuration.ConfigurationManager.AppSettings["mailto"].ToString());
                //set the Subject
                mail.Subject = "Contact us";
                //screen scrape the html
                mail.Body = "<p>Name:" + name + "</p>" +
                    "<p>E-mail:" + email + "</p>" +
                    "<p>Telephone:" + telephone + "</p>" +
                    "<p>content:" + message + "</p>";
                mail.IsBodyHtml = true;
                //send the message
                SmtpClient smtp = new SmtpClient();
                smtp.Send(mail);
                tip = "Send by E-mail success!";
            }
            catch(Exception e)
            {
                tip = e.Message;
            }
        }
        else
        {
            try{
                int parentID =Int32.Parse(currentNode.Current.SelectSingleNode("ContactDocumentType[last()]").GetAttribute("parentID" , ""));
                DocumentType dt = DocumentType.GetByAlias("ContactDocumentType");
                User author = umbraco.BusinessLogic.User.GetUser(0);
                Document doc = Document.MakeNew(name+DateTime.Now.ToString("yyyyMMddHHmmss") , dt , author , parentID);
                doc.getProperty("name").Value = name;
                doc.getProperty("e-mail").Value = email;
                doc.getProperty("telephone").Value = telephone;
                doc.getProperty("message").Value = "<p>"+message+"</p>";
                doc.Publish(author);
                library.UpdateDocumentCache(doc.Id);
                tip = "Already save this information!";
            }
            catch(Exception ex){
                tip = "Failed to save this information";
            } 
        }
    }
}
    <link href="../css/Starterkit.css" rel="stylesheet" type="text/css" />
    <script src="../scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <script src="../scripts/jquery.validate.js" type="text/javascript"></script>
    <style type="text/css">
        .div_contactus
        {
            padding: 4px 10px;
        }
        .p_contactus
        {
            margin: 0;
            padding: 0;
        }
        .required
        {
            color: orange;
            font-weight: bold;
        }
        .text_message
        {
            font-family:Arial;
            font-size:12px;
            padding:4px 4px;
            margin:0px;
            width:100%;
            height:50px;
            border: 1px solid #E9E9E9;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            border-radius: 5px;
            word-wrap: break-word;
            color:#c0c0c0;
        }
        .label_title
        {
            font-weight:normal;
        }
        input.red
        {
            border: 1px solid red;
            color:#FF6600;
        }
        textarea.red 
        {
            border: 1px solid red;
            height:35px;
        }
        label.red
        {
            border:none;
            margin:0;
            padding-left:5px;
            font-weight:normal;
            color: red;
            font-family:Arial;
            font-size:12px;
        }
        
        .input-radius
        {
            font-family:Arial;
            font-size:12px;
            border: 1px solid #E9E9E9;
            padding:2px 4px;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            border-radius: 5px;
            height:25px;
            color:#c0c0c0;
            width:100%;
        }
        .button-radius
        {
            font-family:Arial;
            font-size:14px;
            border: 1px solid #a9a9a9;
            padding:5px 10px;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            border-radius: 5px;
            font-weight:bolder;
        }
        .button-radius:hover
        {
            border: 1px solid #a9a9a9;
            background-color:#808080;
        }
        input:focus
        {
            border:1px solid #666666;
        }
        textarea:focus
        {
            border:1px solid #666666;
        }
    </style>
    <script type="text/javascript">
        jQuery.validator.addMethod(
            "nameisnotmatch",
            function (value, element) {
                return this.optional(element) || value != "Full Name*";
            },
            "This field is required."
        );
        jQuery.validator.addMethod(
            "messageisnotmatch",
            function (value, element) {
                return this.optional(element) || value != "Requirment/Comment*";
            },
            "This field is required."
        );
            $(document).ready(function () {
                //Verify the legitimacy of inputing
                $("#form_contactus").validate({
                    rules: {
                        text_name: {
                            nameisnotmatch: true,
                            required: true
                        },
                        text_email: {
                            required: true,
                            email: true
                        },
                        text_telephone: {
                            digits: true
                        },
                        text_message: {
                            messageisnotmatch: true,
                            required: true
                        }
                    },
                    errorPlacement: function (error, element) {
                        error.appendTo(element.parent().parent().find(".validate_tip"));
                    },
                    errorClass: "red"
                });
                $("#text_name").focusin(function () {
                    var content = $(this).val();
                    if (content == "Full Name*") {
                        $(this).val("");
                        $(this).css("color", "#000000")
                    }
                    if (content == "") {
                        $(this).val("Full Name*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_name").blur(function () {
                    var content = $(this).val();
                    if (content == "Full Name*") {
                        $(this).css("color", "#c0c0c0")
                    }
                    if (content == "") {
                        $(this).val("Full Name*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_email").focusin(function () {
                    var content = $(this).val();
                    if (content == "E-mail*") {
                        $(this).val("");
                        $(this).css("color", "#000000")
                    }
                    if (content == "") {
                        $(this).val("E-mail*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_email").blur(function () {
                    var content = $(this).val();
                    if (content == "E-mail*") {
                        $(this).css("color", "#c0c0c0")
                    }
                    if (content == "") {
                        $(this).val("E-mail*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_telephone").focusin(function () {
                    var content = $(this).val();
                    if (content == "Telephone") {
                        $(this).val("");
                        $(this).css("color", "#000000")
                    }
                    if (content == "") {
                        $(this).val("Telephone");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_telephone").blur(function () {
                    var content = $(this).val();
                    if (content == "Telephone") {
                        $(this).css("color", "#c0c0c0")
                    }
                    if (content == "") {
                        $(this).val("Telephone");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_message").focusin(function () {
                    var content = $(this).val();
                    if (content == "Requirment/Comment*") {
                        $(this).val("");
                        $(this).css("color", "#000000")
                    }
                    if (content == "") {
                        $(this).val("Requirment/Comment*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
                $("#text_message").blur(function () {
                    var content = $(this).val();
                    if (content == "Requirment/Comment*") {
                        $(this).css("color", "#c0c0c0")
                    }
                    if (content == "") {
                        $(this).val("Requirment/Comment*");
                        $(this).css("color", "#c0c0c0")
                    }
                });
            });
    </script>
    <form action="" method="post" id="form_contactus" style="padding:4px 10px 4px 4px">
    <div class="div_contactus">
        <strong>Contact Us</strong>
    </div>
    <div class="div_contactus">
        <p class="p_contactus">
            <input type="text" name="text_name" id="text_name" value="@name" class="input-radius"/>
        </p>
        <span class="validate_tip"></span>
    </div>
    <div class="div_contactus">
        <p class="p_contactus">
            <input type="text" name="text_email" id="text_email" value="@email"  class="input-radius"/></p>
        <span class="validate_tip"></span>
    </div>
    <div class="div_contactus">
        <p class="p_contactus">
            <input type="text" name="text_telephone" id="text_telephone" value="@telephone"  class="input-radius"/></p>
        <span class="validate_tip"></span>
    </div>
    <div class="div_contactus">
        <p class="p_contactus">
            <textarea id="text_message" name="text_message" id="text_message" cols="20" rows="2" class="text_message">@message</textarea></p>
        <span class="validate_tip"></span>
    </div>
    <div class="div_contactus">
        <input type="submit" id="Contact" value="SEND NOW" class="button-radius"/>
        <span style="color:Red">@tip</span>
    </div>
   </form>

